package com.dianzi.certification.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.io.Serializable;

/**
 * @author copa
 * @createDate 2022-03-07 22:13
 * @function
 */
@Data
public class R<T> implements Serializable {

    private static final int STATUS_SUCCESS = 200;
    private static final int STATUS_FAIL = -1;

    private static final String SUCCESS_MSG = "操作成功";
    private static final String ERROR_MSG = "操作失败";

    /**
     * 成功为0，失败为1
     */
    private long code;
    /**
     * 返回的响应信息
     */
    private String msg;
    /**
     * 返回对象信息
     */
    private T data;

    R() {
    }

    R(int code) {
        this.code = code;
    }

    R(int code, T data) {
        this.code = code;
        this.data = data;
    }

    R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    R(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return this.code == STATUS_SUCCESS;
    }

    @JsonIgnore
    public boolean isNotSuccess() {
        return !isSuccess();
    }


    public static <T> R<T> success() {
        return new R<>(STATUS_SUCCESS, SUCCESS_MSG);
    }


    public static <T> R<T> successMsg(String msg) {
        return new R<>(STATUS_SUCCESS, msg);
    }

    public static <T> R<T> success(T data) {
        return new R<>(STATUS_SUCCESS,SUCCESS_MSG, data);
    }

    public static <T> R<T> success(String msg, T data) {
        return new R<>(STATUS_SUCCESS, msg, data);
    }

    public static <T> R<T> error() {
        return new R<>(STATUS_FAIL, ERROR_MSG);
    }

    public static <T> R<T> error(T data) {
        return new R<>(STATUS_FAIL, data);
    }

    public static <T> R<T> errorMsg(String msg) {
        return new R<>(STATUS_FAIL, msg);
    }

    public static <T> R<T> errorCodeMsg(int errorCode, String msg) {
        return new R<>(errorCode, msg);
    }
}
